//
//  WH_TabHeardView.m
//  App_6.23
//
//  Created by wanghao on 16/6/27.
//  Copyright © 2016年 wanghao. All rights reserved.
//

#import "WH_TabHeardView.h"
#import "WH_LabelView.h"
#import "WH_Home.h"

@interface WH_TabHeardView()<UIScrollViewDelegate>

@property (nonatomic, strong) UIImageView *radioImage;
@property (nonatomic, strong) UIButton *fenleiBtn;

@property (nonatomic, strong) UIButton *saidanBtn;

@property (nonatomic, strong) UIButton *wentiBtn;

@property (nonatomic, strong) NSMutableArray *imageArray;
@property (nonatomic, strong) UIScrollView *bigScroll;
@property (nonatomic, strong) NSTimer *myTimer;
@property (nonatomic, assign) NSInteger index;
@property (nonatomic, strong) UIPageControl *pageControl;
@property (nonatomic, strong) NSMutableArray *dataArr;

@property (nonatomic, strong) WH_LabelView *radioLabel;
@end
@implementation WH_TabHeardView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self createBtn];

        [self postData];
    }
    return self;
}
-(void)createBtn{
    
    self.fenleiBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self addSubview:_fenleiBtn];
    _fenleiBtn.backgroundColor = [UIColor blueColor];
#pragma mark - 约束
    [self.fenleiBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    self.fenleiBtn.tag = 100;
    [self.fenleiBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(@25);
        make.top.mas_equalTo(@155);
        make.width.mas_equalTo(@(SCREEN_WIDTH/6-10));
        make.height.mas_equalTo(@(SCREEN_WIDTH/6-10));

    }];
    
    self.saidanBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self addSubview:_saidanBtn];
    _saidanBtn.backgroundColor = [UIColor blueColor];
    [self.saidanBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    self.saidanBtn.tag = 101;
    [self.saidanBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.fenleiBtn);
        make.width.mas_equalTo(@(SCREEN_WIDTH/6-10));
        make.height.mas_equalTo(@(SCREEN_WIDTH/6-10));
        make.centerX.equalTo(self);
        
    }];
    
    self.wentiBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self addSubview:_wentiBtn];
    _wentiBtn.backgroundColor = [UIColor blueColor];
    [self.wentiBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    self.wentiBtn.tag = 102;
    [self.wentiBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.equalTo(@-25);
        make.top.equalTo(self.saidanBtn);
        make.width.mas_equalTo(@(SCREEN_WIDTH/6-10));
        make.height.mas_equalTo(@(SCREEN_WIDTH/6-10));
        
    }];
    [self cerateSubs];
    
    
}

-(void)btnAction:(UIButton *)btn{
    
    [self.delegate pushToClassification:btn.tag];
    
    
}
-(void)cerateSubs{
    
    self.radioImage = [[UIImageView alloc] init];
    _radioImage.backgroundColor = [UIColor redColor];
    [self addSubview:_radioImage];
    [self.radioImage mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.fenleiBtn.mas_bottom).offset(15);
        make.left.mas_equalTo(@20);
        make.width.height.mas_equalTo(@30);
        
    }];

    self.radioLabel = [[WH_LabelView alloc] initWithFrame:CGRectMake(60, SCREEN_WIDTH/6+165, SCREEN_WIDTH-70, 20)];
    [self addSubview:_radioLabel];
#pragma mark - block****

    [self.radioLabel block:^(id result) {
        
        WH_Home *model = result;
        [self.delegate getTapNumber:model];

    }];
    
    UILabel *iLabel = [UILabel new];
    iLabel.backgroundColor = [UIColor colorWithRed:0.91 green:0.91 blue:0.91 alpha:1];
    [self addSubview:iLabel];
    [iLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.radioImage.mas_bottom).offset(10);
        make.left.right.mas_equalTo(@0);
        make.height.mas_equalTo(@10);
        
    }];
    
    UILabel *xiLabel = [UILabel new];
    xiLabel.backgroundColor = [UIColor colorWithRed:0.91 green:0.91 blue:0.91 alpha:1];
    [self addSubview:xiLabel];
    [xiLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.radioImage.mas_top).offset(-10);
        make.left.right.mas_equalTo(@0);
        make.height.mas_equalTo(@1);
        
    }];


}

#pragma mark - labelView&Delegate

-(void)sendmodel:(WH_Home *)model{
    
    [self.delegate getTapNumber:model];
}

-(void)postData{
    
    self.dataArr = [NSMutableArray array];
    NSString *body = [NSString stringWithFormat:@"action=new_win"];
    [WH_AppTools postUrl:URLstr body:body result:HXJSON requestStyle:HXRequestString headerFile:nil success:^(id result) {
        
        // NSLog(@"%@",result);
        NSDictionary *dic = result;
        NSArray *order_array = [dic objectForKey:@"order_array"];
        for (NSDictionary *dic in order_array) {
            
            WH_Home *newest = [WH_Home modelWithDictionary:dic];
            [self.dataArr addObject:newest];
            
        }
        
        [_radioLabel createSubViews:_dataArr];

    } failure:^(NSError *error) {
        
        
    }];
    
    
}


- (void)createSubViews:(NSMutableArray *)images{
    
    
    if (images.count != 0){
            
        
        self.imageArray = images;
        id imageFirst = [self.imageArray firstObject];
        id imageLast = [self.imageArray lastObject];
            
        [self.imageArray insertObject:imageLast atIndex:0];
        [self.imageArray addObject:imageFirst];
            
            
        self.bigScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 150)];
        self.bigScroll.tag = 2016;
        self.bigScroll.pagingEnabled = YES;
        self.bigScroll.bounces = NO;
        self.bigScroll.showsHorizontalScrollIndicator = NO;
        self.bigScroll.delegate = self;
        self.bigScroll.contentSize = CGSizeMake(SCREEN_WIDTH*self.imageArray.count, 150);
        self.bigScroll.contentOffset = CGPointMake(SCREEN_WIDTH, 0);
        //轮播图
        for (int i = 0; i < self.imageArray.count; i++) {
                
                NSString *name = self.imageArray[i];
                UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH*i, 0, SCREEN_WIDTH, 150)];
                //第三方图片赋值
                [imageView sd_setImageWithURL:[NSURL URLWithString:name] placeholderImage:PLACEHOLDER_CELL_SAME];
                
                [self.bigScroll addSubview:imageView];
                
        }
        [self addSubview:self.bigScroll];
            //创建小点
        self.pageControl = [[UIPageControl alloc] init];
        self.pageControl.tag = 2017;
        [self addSubview:self.pageControl];
        
        [self.pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
               
                make.centerX.equalTo(self);
                make.top.equalTo(@120);
                make.width.equalTo(@260);
                make.height.equalTo(@30);
                
        }];
        self.pageControl.numberOfPages = [self.imageArray count] - 2;
        [self.pageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];
        self.pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
        self.pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
        self.myTimer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
        }
        //[self getfocusImagesView];
}


//UIPageControl类型对象中调用的方法
- (void)changePage:(UIPageControl *)page
{
    [self.bigScroll setContentOffset:CGPointMake((page.currentPage + 1) * self.bigScroll.frame.size.width, 0) animated:YES];
}
//使页面上点的变化与视图的变化一致
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    self.pageControl.currentPage = (scrollView.contentOffset.x - self.bigScroll.frame.size.width) / scrollView.frame.size.width;
    
}
-(void)timerAction:(NSTimer *)timer
{
    
    //使用scrollview自带的滚动动画
    [UIView animateWithDuration:1.5 animations:^{
        
        [self.bigScroll setContentOffset:CGPointMake((self.index + 1) * self.bigScroll.frame.size.width, 0)];
        
    }];
    self.index++;
    //如果滑动到最后一页
    if (self.bigScroll.contentOffset.x == (self.imageArray.count-1)*self.bigScroll.frame.size.width) {
        //快速移动到第一页
        [self.bigScroll setContentOffset:CGPointMake(self.bigScroll.frame.size.width, 0)];
        
        self.index = 0;
        
    }
    
}

//定时器在不断循环滚动,就不会减速停止,此方法就不会走
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    if (self.bigScroll.contentOffset.x >= self.bigScroll.frame.size.width * (self.imageArray.count - 1)) {
        
        NSLog(@"最后一页");//循环滚动
        [self.bigScroll setContentOffset:CGPointMake(self.bigScroll.frame.size.width, 0) animated:NO];
    }
    
    if (self.bigScroll.contentOffset.x <= 0) {
        
        [self.bigScroll setContentOffset:CGPointMake(self.bigScroll.frame.size.width * (self.imageArray.count - 2), 0) animated:NO];
    }
    
    //不管怎么移动,保证self.index存的是当前的页码
    self.index = self.bigScroll.contentOffset.x / self.bigScroll.frame.size.width -1;
    
}
//当手指接触的时候让定时器停止
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.myTimer invalidate];
    self.myTimer = nil;
}
//当手指离开的时候开始滑动
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    self.myTimer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
    
}

@end
